<template>
<div id="meta-codeclassmanager">
  <v-header title="代码分类" :showDownloadBtn = "download" :saveShow = "save"></v-header>
  <el-container class="table-manage-box">
    <el-header class="header-tool-bar">
      <!-- 按钮 -->
      <el-row>
        <el-button type="primary" size="mini" @click="adddialogForm">增加</el-button>
        <el-button type="primary" size="mini" @click.stop="removedata">删除</el-button>
        <el-button type="primary" size="mini" @click="handleEdit">编辑</el-button>
        <el-button type="primary" size="mini" @click="refresh">刷新</el-button>
        <span class="condition-query">
          <el-input v-model="condition" size="mini" prefix-icon="el-icon-search" placeholder="请输入内容"
                    clearable></el-input>
          <el-button type="primary" size="mini" @click="queryForPage">查询</el-button>
        </span>
      </el-row>
    </el-header>
    <el-main class="main-table-box">
      <!-- 表单 -->
      <el-table
        ref="table"
        :data="tableData"
        @selection-change="handleSelectionChange"
        @select="checkrow"
        v-loading="tableLoading"
        v-model="checked"
        tooltip-effect="dark"
        style="width: 100%">
        <el-table-column
          type="selection"
          width="auto">
        </el-table-column>
        <el-table-column
          prop="class_id"
          label="分类ID "
          width="auto">
        </el-table-column>
        <el-table-column
          prop="class_desc"
          label="分类描述"
          width="auto">
        </el-table-column>
        <el-table-column
          prop="code_name_cns"
          label="代码中文定义"
          width="auto">
        </el-table-column>
        <el-table-column
          prop="code_name_ens"
          label="代码英文定义"
          width="auto">
        </el-table-column>
        <el-table-column
          prop="code_def_bas"
          label="代码定义依据"
          width="auto">
        </el-table-column>
        <el-table-column
          prop="code_source"
          label="代码取值来源"
          width="auto">
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCount">
        </el-pagination>
    </el-main>
  </el-container>
  <!-- 新增/编辑-弹窗 -->
    <el-dialog :title="addRoleDialogMode === 'ADD' ? '新增数据' : '编辑数据'"
               :visible.sync="addRoleDialogVisiable"
               v-model="addRoleDialogMode"
               @close="cancel"
               >
      <el-form :model="ruleForm" :rules="rules"  ref="ruleForm" label-width="150px" class="demo-ruleForm">
        <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="标准代码ID" prop="code_id">
            <el-input v-model="ruleForm.code_id" :disabled="editmetaid" id="input-height"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="代码中文定义" prop="code_name_cns">
          <el-input v-model="ruleForm.code_name_cns"
          :disabled="editsysid" id="input-height"></el-input>
        </el-form-item>
        </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="分类描述" prop="class_desc">
            <el-input v-model="ruleForm.class_desc" id="input-height"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类ID " prop="class_id">
            <el-input v-model="ruleForm.class_id" id="input-height"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
           <el-form-item label="代码长度" prop="code_length">
           <el-input v-model="ruleForm.code_length" id="input-height"></el-input>
           </el-form-item>
          </el-col>
            <el-col :span="12">
          <el-form-item label="代码取值来源" prop="code_source">
           <el-input v-model="ruleForm.code_source" id="input-height"></el-input>
          </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="代码英文定义" prop="code_name_ens">
           <el-input v-model="ruleForm.code_name_ens" id="input-height"></el-input>
           </el-form-item>
          </el-col>
          <el-col :span="12">
          <el-form-item label="代码编码层级" prop="code_enc_hac">
            <el-input v-model="ruleForm.code_enc_hac" id="input-height"></el-input>
          </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
          <el-form-item label="代码层级关系" prop="code_hac_rs">
           <el-input v-model="ruleForm.code_hac_rs" id="input-height"></el-input>
          </el-form-item>
          </el-col>
          <el-col :span="12">
           <el-form-item label="代码中文使用说明" prop="code_use_cns">
            <el-input v-model="ruleForm.code_use_cns" id="input-height"></el-input>
           </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
          <el-form-item label="代码英文使用说明" prop="code_use_ens">
           <el-input v-model="ruleForm.code_use_ens" id="input-height"></el-input>
          </el-form-item>
          </el-col>
          <el-col :span="12">
           <el-form-item label="代码定义依据" prop="code_def_bas">
            <el-input v-model="ruleForm.code_def_bas" id="input-height"></el-input>
           </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
        <el-col>
           <el-form-item label="代码管理状态" prop="code_state">
            <el-select v-model="ruleForm.code_state" placeholder="请选择" id="input-width">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
            </el-form-item>
        </el-col>
        </el-row>
        <el-form-item id="dialogbtn">
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button @click="cancel('ruleForm')">关闭</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
</div>
</template>

<script>
  import ajax from '../../../../api/ajax/ajax'
  import { VHeader } from '../../../../components'
  import {oneValidator, threeValidator, numLetteValidator} from '../../../../assets/js/rules'
  import api from '../../../../api/url'
export default{
  name: 'meta-codeclassmanager',
  components: {
    VHeader
  },
  data () {
      return {
        // 代码状态管理
        options: [{
          value: '0',
          label: '无效'
        }, {
          value: '1',
          label: '有效'
        }],
        tableLoading: true,
        condition: '', // 输入条件
        addRoleDialogVisiable: false,
        editdialogform: false,
        currentPage: 1, // 当前页码
        pagesize: 10, // 默认每页数据量
        totalCount: 0, // 默认数据总数
        editmetaid: false, // meta_id input框禁止修改
        editsysid: false, // event_id input框禁止修改
        checked: false,
        tableData: [],
        checkbox: [],
        addRoleDialogMode: 'ADD', // 对话框色对话框模式  ADD新增 MODIFY 修改
        list: [],
        download: false,
        save: false,
        ruleForm: {
          class_id: '',
          code_name_cns: '',
          code_length: '',
          class_desc: '',
          code_hac_rs: '',
          code_use_cns: '',
          code_use_ens: '',
          code_def_bas: '',
          code_source: '',
          code_state: '',
          code_id: '',
          code_name_ens: '',
          code_enc_hac: ''
        },
        rules: {
          class_id: [
            { required: true, message: '请输入', trigger: 'blur' },
            { max: 3, message: '请输入3个字符', trigger: 'blur' },
            { validator: numLetteValidator, trigger: 'blur' }
          ],
           code_length: [
            { required: true, message: '请输入', trigger: 'blur' },
           { max: 3, message: '最多输入3个字符', trigger: 'blur' },
           { validator: threeValidator, trigger: 'blur' }
          ],
          class_desc: [
            { required: true, message: '请输入', trigger: 'blur' },
             { max: 60, message: '最多输入60个字符', trigger: 'blur' }
          ],
          code_id: [
            { required: true, message: '请输入', trigger: 'blur' },
            { max: 7, message: '最多输入7个字符', trigger: 'blur' },
            { validator: numLetteValidator, trigger: 'blur' }
          ],
          code_name_cns: [
            { required: true, message: '请输入', trigger: 'blur' },
             { max: 20, message: '最多输入20个字符', trigger: 'blur' }
          ],
          code_use_ens: [
            { required: true, message: '请输入', trigger: 'blur' },
             { max: 60, message: '最多输入60个字符', trigger: 'blur' }
          ],
          code_hac_rs: [
            { required: true, message: '请输入', trigger: 'blur' },
            { max: 9, message: '最多输入9个字符', trigger: 'blur' },
            { validator: numLetteValidator, trigger: 'blur' }
          ],
          code_use_cns: [
            { required: true, message: '请输入', trigger: 'blur' },
            { max: 60, message: '最多输入60个字符', trigger: 'blur' }
          ],
          code_state: [
            { required: true, message: '请选择', trigger: 'blur' },
            { max: 1, message: '最多输入1个字符', trigger: 'blur' }
          ],
          code_source: [
            { required: true, message: '请输入', trigger: 'blur' },
            { max: 60, message: '最多输入60个字符', trigger: 'blur' }
          ],
          code_def_bas: [
            { required: true, message: '请输入', trigger: 'blur' },
            { max: 2, message: '最多输入2个字符', trigger: 'blur' },
            { validator: numLetteValidator, trigger: 'blur' }
          ],
          code_name_ens: [
            { required: true, message: '请输入', trigger: 'blur' },
            { max: 20, message: '最多输入20个字符', trigger: 'blur' }
          ],
          code_enc_hac: [
            { required: true, message: '请输入', trigger: 'blur' },
            { max: 1, message: '最多输入1个数字字符', trigger: 'blur' },
            { validator: oneValidator, trigger: 'blur' }
          ]
        },
        currentItem: {}
      }
    },
    created () {
      this.queryForPage()
    },
    methods: {
      // 每页显示条目个数
      handleSizeChange (val) {
        this.pagesize = val
        this.handleCurrentChange()
      },
      queryForPage () {
         const pagelistdata = {
          keyword: this.condition,
          pageNo: this.currentPage,
          pageSize: this.pagesize
        }
      // 查询请求
        ajax.post(api.SELECT_META_CODE_CLASS_LIST_FOR_PAGE, pagelistdata).then((res) => {
          this.tableLoading = false
          this.tableData = []
          this.tableData = res.data.list
          this.totalCount = res.data.total
          this.checked = false // 清除选中状态
        })
      },
      // 当前页数
      handleCurrentChange (val) {
        this.currentPage = val
        this.queryForPage()
      },
      // 增加按钮
      adddialogForm () {
        this.ruleForm = {}
        this.currentItem = {}
        this.$refs.table.clearSelection() // 点击新增清除选中状态
        this.checked = false
        this.addRoleDialogVisiable = true
        this.addRoleDialogMode = 'ADD'
        this.editmetaid = false
        this.editsysid = false
      },
      // 关闭
      cancel () {
        // this.$refs.ruleForm.clearValidate()
        // this.ruleForm = {}
        this.$refs.ruleForm.resetFields() // 移除校验 重置为初始状态
        this.addRoleDialogVisiable = false
        this.editdialogform = false
        this.ruleForm = this.currentItem
        this.checked = false
        this.handleSizeChange()
      },
         // 重置
      resetForm (formName) {
       this.ruleForm = {...this.currentItem}
      },
      // 删除数据
      removedata () {
        if (this.list.length > 0) {
          this.$confirm('是否删除该记录?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let remdata = {
              key_list: this.list
            }
            // 删除请求
            ajax.post(api.DELETE_META_CODE_CLASS, remdata).then((res) => {
              this.handleSizeChange()
            })
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }).catch(() => {
            // 去掉取消删除提示
            // this.$message({
            //   type: 'info',
            //   message: '已取消删除'
            // })
          })
        } else {
          this.$message.error('请至少选择一条记录进行删除')
        }
      },
      handleSelectionChange (val) {
        this.list = []
        for (let i = 0; i < val.length; i++) {
          let remdata = {
            code_id: val[i].code_id
          }
          this.list.push(remdata)
        }
      },
      checkrow (row) {
        if (row.length > 0) {
          this.ruleForm = row[0]
          this.checkbox = row
          this.checked = true
        } else {
          this.checked = false
        }
      },
      // 编辑
      handleEdit (row) {
        // 判断是否勾选
        if (this.checked === true) {
          if (this.checkbox.length < 2) {
            this.addRoleDialogVisiable = true // 显示弹框
            this.editmetaid = true // class_id input框禁止修改
            this.editsysid = false // code_enc_hac input框禁止修改
            this.addRoleDialogMode = 'MODIFY' // 新增角色对话框模式 编辑模式
             this.currentItem = JSON.parse(JSON.stringify(this.ruleForm))
            this.ruleForm = JSON.parse(JSON.stringify(this.ruleForm))
          } else {
            this.$message.error('只能选择一条')
          }
        } else {
          this.$message.error('请至少选择一条记录进行编辑')
        }
      },
      refresh () {
        this.handleSizeChange()
      },
      // 新增/修改 元事件
      submitForm (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
              const insertdata = {
                class_id: this.ruleForm.class_id,
                code_name_cns: this.ruleForm.code_name_cns,
                code_length: this.ruleForm.code_length,
                class_desc: this.ruleForm.class_desc,
                code_enc_hac: this.ruleForm.code_enc_hac,
                code_id: this.ruleForm.code_id,
                code_hac_rs: this.ruleForm.code_hac_rs,
                code_def_bas: this.ruleForm.code_def_bas,
                code_source: this.ruleForm.code_source,
                code_state: this.ruleForm.code_state,
                code_name_ens: this.ruleForm.code_name_ens,
                code_use_cns: this.ruleForm.code_use_cns,
                code_use_ens: this.ruleForm.code_use_ens
              }
              if (this.addRoleDialogMode === 'ADD') {
                // 新增请求
                ajax.post(api.INSERT_META_CODE_CLASS, insertdata).then((res) => {
                  this.handleSizeChange()
                   this.$message({
                   type: 'success',
                   message: '添加成功!'
              })
                })
              } else if (this.addRoleDialogMode === 'MODIFY') {
                // 修改请求
                ajax.post(api.UPDATE_META_CODE_CLASS, insertdata).then((res) => {
                  this.ruleForm = {}
                  this.handleSizeChange()
                   this.$message({
                    type: 'success',
                    message: '编辑成功!'
              })
                })
              }
              this.addRoleDialogVisiable = false
          } else {
            return false
          }
        })
      }
    }
}
</script>
<style lang="stylus" rel="stylesheet/stylus">
  #meta-codeclassmanager
    height 100%
    position relative
    .table-manage-box
      height 100%
      padding 10px 0px 20px 10px
      .header-tool-bar
        line-height 50px
        vert-align middle
        background-color #FFFFFF
        margin-bottom 3px
        .condition-query
          width 300px
          float right
          margin-right -10px
        .el-input--mini
          width 200px
        .el-button--danger
          background-color #016DFB
          border-color #016DFB
        .dialog-input
          width 210px
          height 30px
          border-radius 4px
          border 1px solid #D8DCE5
      .main-table-box
        padding 0px 0px
        height 100%
        background-color #fff
        .el-table
          padding-bottom 74px
        .el-pagination
          z-index 5
          height 36px
          background-color #fff
          position absolute
          width 100%
          bottom 0px
          padding-bottom 4px
          border-top solid 1px #f4f4f4
    .el-dialog
      width 900px
      padding-right 30px
      .el-dialog__body
        margin 0 auto
        padding 20px 20px
        .btn-center
          text-align center
  #dialogbtn
      margin-left 440px
  #input-height
      height 40px
  #input-width
      padding 20px 74px
</style>
